using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Plugin;
using MicroMsg.Resource.@string;
using MicroMsg.Scene;
using MicroMsg.Storage;
using MicroMsg.UI.UserContrl;
using MicroMsg.UI.Util;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace MicroMsg.UI.Page
{
	public class QQMailSessionView : ChatContainerPage
	{
		private const string TAG = "QQMailSessionView";
		private const int READ_COUNT = 5;
		private const string ScrollingState = "Scrolling";
		private const string NotScrollingState = "NotScrolling";
		private MMObservableCollection<ChatMsg, ChatListItem> mDataList = new MMObservableCollection<ChatMsg, ChatListItem>();
		private EventWatcher mListChangeWatcher;
		public static QQMail delList;
		private int unReadCount;
		private ScrollViewer mScrollViewer;
		private bool mBIsScrollHandlerSet;
		private double oldOffset;
		private Point pointLeave;
		private Point pointEnter;
		private ApplicationBar mBar;
		private List<UIElement> mUiToHideList;
		internal Grid LayoutRoot;
		internal Grid LayoutContent;
		internal TextBlock panelTitle;
		internal Canvas canvasProgressBar;
		internal PerformanceProgressBar ProgressBar;
		internal Grid listView;
		internal StackPanel ContentPanel;
		internal MMListBox QQMailSessionlist;
		internal Grid NoMemberView;
		internal TextBlock NoMemberTitle;
		internal Border borderLine;
		internal TextBlock textDes;
		private bool _contentLoaded;
		public bool IsScrolling
		{
			get;
			private set;
		}
		public QQMailSessionView()
		{
			this.InitializeComponent();
			base.add_Loaded(new RoutedEventHandler(this.QQMailSessionView_loaded));
			base.add_Unloaded(new RoutedEventHandler(this.QQMailSessionView_unloaded));
			this.QQMailSessionlist.LoadedComplete += new RoutedEventHandler(this.QQMailList_LoadedComplete);
		}
		private void QQMailSessionView_loaded(object sender, RoutedEventArgs e)
		{
			this.setScrollHandler();
			this.QQMailSessionlist.IsAutoToBottom = true;
		}
		private void QQMailSessionView_unloaded(object sender, RoutedEventArgs e)
		{
		}
		private void QQMailList_LoadedComplete(object sender, RoutedEventArgs e)
		{
		}
		protected override void OnPageIn(object[] object_0)
		{
			if (object_0 != null && object_0.Length > 0)
			{
				this.unReadCount = (int)object_0[0];
			}
			this.InitQQMailSessionView();
			this.InitWatcher();
			this.ClearUnreadCount();
			Touch.add_FrameReported(new TouchFrameEventHandler(this.Touch_FrameReported));
		}
		private void InitWatcher()
		{
			if (this.mListChangeWatcher == null)
			{
				this.mListChangeWatcher = new EventWatcher(this, this, new EventHandlerDelegate(this.onNewsConversationChange));
				EventCenter.registerEventWatcher(EventConst.ON_STORAGE_CHAT_MSG_ADD, this.mListChangeWatcher);
				EventCenter.registerEventWatcher(EventConst.ON_STORAGE_CHAT_MSG_DEL, this.mListChangeWatcher);
			}
		}
		private void UnitWatcher()
		{
			if (this.mListChangeWatcher != null)
			{
				EventCenter.removeEventWatcher(EventConst.ON_STORAGE_CHAT_MSG_ADD, this.mListChangeWatcher);
				EventCenter.removeEventWatcher(EventConst.ON_STORAGE_CHAT_MSG_DEL, this.mListChangeWatcher);
				this.mListChangeWatcher = null;
			}
		}
		private void onNewsConversationChange(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs == null)
			{
				return;
			}
			List<ChatMsg> listObject = evtArgs.getListObject<ChatMsg>();
			if (listObject == null)
			{
				return;
			}
			List<ChatMsg> list = new List<ChatMsg>();
			if (evtArgs.mEventID == EventConst.ON_STORAGE_CHAT_MSG_ADD)
			{
				using (List<ChatMsg>.Enumerator enumerator = listObject.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						ChatMsg current = enumerator.get_Current();
						if (current != null && current.strTalker.Equals(ConstantValue.TAG_QQMAIL))
						{
							list.Add(current);
						}
					}
					goto IL_C3;
				}
			}
			if (evtArgs.mEventID == EventConst.ON_STORAGE_CHAT_MSG_DEL)
			{
				using (List<ChatMsg>.Enumerator enumerator2 = listObject.GetEnumerator())
				{
					while (enumerator2.MoveNext())
					{
						ChatMsg current2 = enumerator2.get_Current();
						if (current2 != null && current2.strTalker.Equals(ConstantValue.TAG_QQMAIL))
						{
							this.ConversationDelete(current2);
						}
					}
				}
				return;
			}
			IL_C3:
			if (list.get_Count() > 0)
			{
				this.ConversationUpdate(new ObservableCollection<ChatMsg>(list), list.get_Count(), false);
			}
			this.ClearUnreadCount();
		}
		private void InitQQMailSessionView()
		{
			base.set_ApplicationBar(this.GetApplicationBar());
			this.QQMailSessionlist.IsAutoToBottom = true;
			int num = 5;
			int msgNum = StorageMgr.chatMsg.getMsgNum(ConstantValue.TAG_QQMAIL);
			int start = (msgNum > 5) ? (msgNum - num) : 0;
			this.ConversationUpdate(QQMailMgr.getMailChatList(start, num), msgNum, true);
			this.mDataList.Bind(this.QQMailSessionlist);
			this.panelTitle.set_Text(strings.Plugin_qqmail_title);
			if (this.mDataList.get_Count() <= 0 || msgNum <= 0)
			{
				Log.smethod_5("QQMailSessionView", "mDataList.Count <= 0 No Data");
				this.ChangeToNoMsgView();
			}
		}
		private void ConversationUpdate(ObservableCollection<ChatMsg> cvsList, int totalCount, bool isHistoryNews = true)
		{
			if (cvsList == null)
			{
				return;
			}
			if (this.mDataList.get_Count() > 0)
			{
				this.mDataList[0] = this.mDataList[0];
			}
			for (int i = cvsList.get_Count() - 1; i >= 0; i--)
			{
				ChatMsg chatMsg_ = cvsList.get_Item(i);
				this.ConversationUpdate(chatMsg_, isHistoryNews);
			}
			int arg_58_0 = this.mDataList.get_Count();
		}
		public void ConversationUpdate(ChatMsg chatMsg_0, bool isHistoryNews)
		{
			if (chatMsg_0 == null)
			{
				return;
			}
			if (!isHistoryNews && this.mDataList.get_Count() > 0)
			{
				this.mDataList.Insert(this.mDataList.get_Count(), chatMsg_0);
			}
			else
			{
				this.mDataList.Insert(0, chatMsg_0);
			}
			if (this.mDataList.get_Count() > 0 && this.listView.get_Visibility() == 1)
			{
				this.ChangeToListView();
			}
		}
		private void ScrollToLast()
		{
			this.QQMailSessionlist.ScrollToLast();
		}
		private void ClearUnreadCount()
		{
			Conversation conversation = StorageMgr.converation.method_0(ConstantValue.TAG_QQMAIL);
			if (conversation == null || conversation.nUnReadCount <= 0)
			{
				return;
			}
			bool forcetoReaded = true;
			if (this.mDataList != null && this.mDataList.get_Count() != 0)
			{
				ConversationMgr.setUnreadCount(ConstantValue.TAG_QQMAIL, 0, forcetoReaded);
				return;
			}
		}
		public void ConversationDelete(ChatMsg mail)
		{
			int msgNum = StorageMgr.chatMsg.getMsgNum(ConstantValue.TAG_QQMAIL);
			if (msgNum == 0)
			{
				this.ChangeToNoMsgView();
				QQMailMgr.delMainConversation();
				return;
			}
			for (int i = 0; i < this.mDataList.get_Count(); i++)
			{
				ChatMsg chatMsg = this.mDataList[i];
				if (chatMsg.nMsgLocalID == mail.nMsgLocalID)
				{
					this.mDataList.RemoveAt(i);
					IL_60:
					if (this.mDataList.get_Count() == 0)
					{
						this.ConversationUpdate(QQMailMgr.getMailChatList(msgNum - 1, 1), msgNum, true);
					}
					return;
				}
			}
			goto IL_60;
		}
		private void QQMailSessionItem_ItemClick(object sender, object object_0)
		{
			QQMail qQMail = object_0 as QQMail;
			if (qQMail == null)
			{
				return;
			}
			PhoneApplicationFrame phoneApplicationFrame = Application.get_Current().get_RootVisual() as PhoneApplicationFrame;
			BasePage basePage = phoneApplicationFrame.get_Content() as BasePage;
			basePage.GoToPage("/Source/Plugin/QQMail/UI/QQMailDetailInfoView.xaml", new object[]
			{
				qQMail
			}, false);
		}
		private void setScrollHandler()
		{
			if (this.mBIsScrollHandlerSet)
			{
				return;
			}
			this.mBIsScrollHandlerSet = true;
			this.mScrollViewer = (ScrollViewer)UIElementTraveler.FindElementRecursive(this.QQMailSessionlist, typeof(ScrollViewer));
			if (this.mScrollViewer == null)
			{
				return;
			}
			FrameworkElement frameworkElement = VisualTreeHelper.GetChild(this.mScrollViewer, 0) as FrameworkElement;
			if (frameworkElement != null)
			{
				VisualStateGroup visualStateGroup = UIElementTraveler.FindVisualState(frameworkElement, "ScrollStates");
				if (visualStateGroup != null)
				{
					visualStateGroup.add_CurrentStateChanging(new EventHandler<VisualStateChangedEventArgs>(this.OnScrollStateChanging));
					visualStateGroup.add_CurrentStateChanged(new EventHandler<VisualStateChangedEventArgs>(this.OnScrollStateChanged));
				}
			}
		}
		private void OnScrollStateChanged(object sender, VisualStateChangedEventArgs e)
		{
			if (this.ProgressBar.IsIndeterminate)
			{
				this.ProgressBar.IsIndeterminate = false;
			}
		}
		private void OnScrollStateChanging(object sender, VisualStateChangedEventArgs e)
		{
			this.QQMailSessionlist.IsAutoToBottom = false;
			this.IsScrolling = (e.get_NewState().get_Name() == "Scrolling");
			if (e.get_NewState().get_Name() == "NotScrolling")
			{
				this.tryGetHistoryNews();
			}
		}
		private void Touch_FrameReported(object sender, TouchFrameEventArgs e)
		{
			TouchPointCollection touchPoints = e.GetTouchPoints(null);
			if (1 != touchPoints.get_Count())
			{
				try
				{
					TouchPoint primaryTouchPoint = e.GetPrimaryTouchPoint(null);
					if (primaryTouchPoint != null)
					{
						this.TouchLeave(primaryTouchPoint.get_Position());
					}
				}
				catch (Exception)
				{
					return;
				}
			}
			TouchPoint touchPoint = null;
			try
			{
				touchPoint = e.GetPrimaryTouchPoint(null);
			}
			catch (Exception)
			{
				return;
			}
			if (touchPoint != null)
			{
				if (touchPoint.get_Action() == 1)
				{
					this.TouchEnter(touchPoint.get_Position());
					return;
				}
				if (touchPoint.get_Action() == 2)
				{
					return;
				}
				if (touchPoint.get_Action() == 3)
				{
					this.TouchLeave(touchPoint.get_Position());
				}
				return;
			}
		}
		private void TouchLeave(Point point)
		{
			this.pointLeave = point;
		}
		private void TouchEnter(Point point)
		{
			this.pointEnter = point;
		}
		private double getScrollDistance()
		{
			return this.pointLeave.get_Y() - this.pointEnter.get_Y();
		}
		private int tryGetHistoryNews()
		{
			if (this.mScrollViewer != null && this.mDataList != null)
			{
				if ((this.oldOffset - 0.0001 <= 0.0 && this.getScrollDistance() - 150.0 > 0.0) || (this.mScrollViewer.get_VerticalOffset() - 1E-06 <= 0.0 && this.getScrollDistance() - 500.0 > 0.0))
				{
					this.oldOffset = this.mScrollViewer.get_VerticalOffset();
					this.ProgressBar.IsIndeterminate = true;
					Deployment.get_Current().get_Dispatcher().BeginInvoke(delegate
					{
						this.doInvoke();
					});
				}
				else
				{
					this.oldOffset = this.mScrollViewer.get_VerticalOffset();
				}
				return 0;
			}
			return 0;
		}
		private void doInvoke()
		{
			int msgNum = StorageMgr.chatMsg.getMsgNum(ConstantValue.TAG_QQMAIL);
			int count = this.mDataList.get_Count();
			if (msgNum > count)
			{
				int num = (msgNum - count > 5) ? 5 : (msgNum - count);
				ObservableCollection<ChatMsg> mailChatList = QQMailMgr.getMailChatList(msgNum - count - num, num);
				this.ConversationUpdate(mailChatList, msgNum, true);
				if (mailChatList.get_Count() > 0)
				{
					double num2 = (double)mailChatList.get_Count() * 1.0 - 0.5;
					this.oldOffset = num2;
				}
			}
		}
		public ApplicationBar GetApplicationBar()
		{
			if (this.mBar != null)
			{
				return this.mBar;
			}
			this.mBar = new ApplicationBar();
			this.mBar.set_IsMenuEnabled(true);
			this.mBar.set_IsVisible(true);
			this.mBar.set_Opacity(0.7);
			ApplicationBarIconButton applicationBarIconButton = new ApplicationBarIconButton(new Uri("/Source/Plugin/QQMail/Resource/edit_mail.png", 2));
			applicationBarIconButton.set_Text(strings.Plugin_qqmail_mail);
			applicationBarIconButton.add_Click(new EventHandler(this.writeMailButton_Click));
			this.mBar.get_Buttons().Add(applicationBarIconButton);
			ApplicationBarIconButton applicationBarIconButton2 = new ApplicationBarIconButton(new Uri("/Source/Plugin/Common/Resource/Img/Plugin_Setting.png", 2));
			applicationBarIconButton2.set_Text(strings.Set_Plugin_Setting);
			applicationBarIconButton2.add_Click(new EventHandler(this.pluginSettingButton_Click));
			this.mBar.get_Buttons().Add(applicationBarIconButton2);
			return this.mBar;
		}
		private void writeMailButton_Click(object sender, EventArgs e)
		{
			PhoneApplicationFrame phoneApplicationFrame = Application.get_Current().get_RootVisual() as PhoneApplicationFrame;
			BasePage basePage = phoneApplicationFrame.get_Content() as BasePage;
			BasePage arg_2B_0 = basePage;
			string arg_2B_1 = "/Source/Plugin/QQMail/UI/QQMailNewMailView.xaml";
			object[] object_ = new object[1];
			arg_2B_0.GoToPage(arg_2B_1, object_, false);
		}
		private void pluginSettingButton_Click(object sender, EventArgs e)
		{
			base.GoToPage("/Source/Plugin/PluginSettingPage.xaml", new object[]
			{
				PluginManager.findPluginByName("TXQQMail")
			}, false);
			int i = 0;
			while (i < PageManager.getInstance().basePageList.get_Count())
			{
				if (!(PageManager.getInstance().basePageList.get_Item(i) is PluginSettingPage))
				{
					i++;
				}
				else
				{
					PluginSettingPage pluginSettingPage = PageManager.getInstance().basePageList.get_Item(i) as PluginSettingPage;
					if (pluginSettingPage.GetPluginName() == "TXQQMail")
					{
						PageManager.getInstance().PopPage();
						return;
					}
					return;
				}
			}
		}
		private void ChangeToNoMsgView()
		{
			if (this.listView.get_Visibility() == 1)
			{
				return;
			}
			this.listView.set_Visibility(1);
			this.NoMemberView.set_Visibility(0);
		}
		private void ChangeToListView()
		{
			if (this.NoMemberView.get_Visibility() == 1)
			{
				return;
			}
			this.NoMemberView.set_Visibility(1);
			this.listView.set_Visibility(0);
		}
		public override List<UIElement> GetBkContents()
		{
			if (this.mUiToHideList == null)
			{
				this.mUiToHideList = new List<UIElement>();
				this.mUiToHideList.Add(this.LayoutContent);
			}
			return this.mUiToHideList;
		}
		public override Panel GetChatContainerPanel()
		{
			return this.LayoutRoot;
		}
		protected override void resumeFromChat()
		{
			UIDelayService.getInstance().DoDelayAction();
		}
		protected override void OnBackKeyPress(CancelEventArgs cancelEventArgs_0)
		{
			if (base.PreOnBackKeyPress(cancelEventArgs_0))
			{
				return;
			}
			if (!cancelEventArgs_0.get_Cancel())
			{
				cancelEventArgs_0.set_Cancel(true);
				ItemFlyInAndOutAnimations.TitleFlyOut(this);
				base.get_Dispatcher().BeginInvoke(delegate
				{
					base.GoBack();
				});
			}
			base.OnBackKeyPress(cancelEventArgs_0);
		}
		protected override void OnPageOut()
		{
			base.PreOnPageOut();
			Touch.remove_FrameReported(new TouchFrameEventHandler(this.Touch_FrameReported));
			this.UnitWatcher();
			ServiceCenter.statReportMgr.addStatItem(enKVStatKey.MM_QQMAILCLICKCOUNT, "1");
		}
		protected override void OnPageForward(Uri targetPage)
		{
			base.PreOnPageForward(targetPage);
		}
		protected override void OnPageBack()
		{
			base.PreOnPageBack();
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/Plugin/QQMail/UI/QQMailSessionView.xaml", 2));
			this.LayoutRoot = (Grid)base.FindName("LayoutRoot");
			this.LayoutContent = (Grid)base.FindName("LayoutContent");
			this.panelTitle = (TextBlock)base.FindName("panelTitle");
			this.canvasProgressBar = (Canvas)base.FindName("canvasProgressBar");
			this.ProgressBar = (PerformanceProgressBar)base.FindName("ProgressBar");
			this.listView = (Grid)base.FindName("listView");
			this.ContentPanel = (StackPanel)base.FindName("ContentPanel");
			this.QQMailSessionlist = (MMListBox)base.FindName("QQMailSessionlist");
			this.NoMemberView = (Grid)base.FindName("NoMemberView");
			this.NoMemberTitle = (TextBlock)base.FindName("NoMemberTitle");
			this.borderLine = (Border)base.FindName("borderLine");
			this.textDes = (TextBlock)base.FindName("textDes");
		}
	}
}
